package jeecg.business.controller.report;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.ComboTree;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.common.model.json.TreeGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.tag.vo.easyui.ComboTreeModel;
import org.jeecgframework.tag.vo.easyui.TreeGridModel;

import jeecg.system.pojo.base.TSDepart;
import jeecg.system.pojo.base.TSType;
import jeecg.system.pojo.base.TSTypegroup;
import jeecg.system.service.SystemService;
import org.jeecgframework.core.util.MyBeanUtils;

import jeecg.business.entity.report.ReportCatalogEntity;
import jeecg.business.service.report.ReportCatalogServiceI;

/**   
 * @Title: Controller
 * @Description: 简报目录
 * @author LinYingPiaoJian
 * @date 2015-09-29 19:09:24
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/reportCatalogController")
public class ReportCatalogController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ReportCatalogController.class);

	@Autowired
	private ReportCatalogServiceI reportCatalogService;
	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 简报目录列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "reportCatalog")
	public ModelAndView reportCatalog(HttpServletRequest request) {
		String window = request.getParameter("window");
		request.setAttribute("window", window);
		String qyid = request.getParameter("qyid");
		request.setAttribute("qyid", qyid);
		return new ModelAndView("jeecg/business/report/reportCatalogList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(ReportCatalogEntity reportCatalog,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(ReportCatalogEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, reportCatalog);
		this.reportCatalogService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}
	
	/**
	 * 权限列表
	 */
	@RequestMapping(params = "datatree")
	@ResponseBody
	public List<TreeGrid> typetree(HttpServletRequest request, TreeGrid treegrid) {
		String qyid =request.getParameter("qyid");
		String hql = " from ReportCatalogEntity where qyid = '"+qyid+"' " ;
		if (treegrid.getId() != null) {
			hql += " and pid = '"+treegrid.getId()+"' ";
		}
		if (treegrid.getId() == null) {
			hql += " and( pid is null or pid = '')";
		}
		hql += " order by sxh";
		List<TSType> typelist = systemService.findByQueryString(hql);
		List<TreeGrid> treeGrids = new ArrayList<TreeGrid>();
		TreeGridModel treeGridModel = new TreeGridModel();
		treeGridModel.setTextField("mc");
		treeGridModel.setParentText("preportCatalogEntity_mc");
		treeGridModel.setParentId("preportCatalogEntity_id");
		treeGridModel.setIdField("id");
		treeGridModel.setChildList("reportCatalogs");
		treeGridModel.setOrder("sxh");
		treeGridModel.setSrc("mc");
		treeGrids = systemService.treegrid(typelist, treeGridModel);
		return treeGrids;
	}

	@RequestMapping(params = "setPFunction")
	@ResponseBody
	public List<ComboTree> setPFunction(ReportCatalogEntity reportCatalog , HttpServletRequest request,HttpServletResponse response, ComboTree comboTree) {
		AjaxJson j = new AjaxJson();
		
		CriteriaQuery cq = new CriteriaQuery(ReportCatalogEntity.class);
		
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, reportCatalog);
		String pid = comboTree.getId() == null ?"" : comboTree.getId();
		
		String hql = " from ReportCatalogEntity where qyid = '"+reportCatalog.getQyid()+"' and pid ='"+pid+"'";
		hql += " order by sxh";
		List<ReportCatalogEntity> rlist = systemService.findByQueryString(hql);
		List<ComboTree> comboTrees = new ArrayList<ComboTree>();
		String key = "id";
		ComboTreeModel comboTreeModel = new ComboTreeModel(key, "mc", "reportCatalogs");	
		
		comboTreeModel.setState("open");
		
		comboTrees = systemService.ComboTree(rlist, comboTreeModel,null,true);
		
		return comboTrees;
		
//		File file = new File("E:\\my_doc\\jquery-easyui-1.3.2\\demo\\combotree\\tree_data1.json");
//		byte [] bytes = new byte[2048];
//		try {
//			new FileInputStream(file).read(bytes);
//		} catch (FileNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		String json = new String(bytes).trim();
//		System.out.println(json);
//		PrintWriter out;
//		try {
//			out = response.getWriter();
//			out.print(json);
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
	}
	
	/**
	 * 删除简报目录
	 * 
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(ReportCatalogEntity reportCatalog, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		reportCatalog = systemService.getEntity(ReportCatalogEntity.class, reportCatalog.getId());
		message = "删除成功";
		reportCatalogService.delete(reportCatalog);
		systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		
		j.setMsg(message);
		return j;
	}


	/**
	 * 添加简报目录
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(ReportCatalogEntity reportCatalog, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		if (StringUtil.isNotEmpty(reportCatalog.getId())) {
			message = "更新成功";
			ReportCatalogEntity t = reportCatalogService.get(ReportCatalogEntity.class, reportCatalog.getId());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(reportCatalog, t);
				reportCatalogService.saveOrUpdate(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			message = "添加成功";
			//设置codetype+上级id，每级3位
			String id = reportCatalog.getQyid();
			if(reportCatalog.getPreportCatalogEntity() != null && StringUtil.isNotEmpty(reportCatalog.getPreportCatalogEntity().getId())) id =reportCatalog.getPreportCatalogEntity().getId();
			String sql = " select max(id) as MAXID from nms_report_catalog where qyid = '"+reportCatalog.getQyid()+"' and id like '"+id+"___'";
			String maxid = (String)systemService.findOneForJdbc(sql).get("MAXID");
			if(StringUtil.isNotEmpty(maxid)){				
				Integer mid = Integer.parseInt(maxid.replace(id, "").replace(reportCatalog.getQyid(), ""));
				mid++;
				if(mid < 100) id+="0";
				if(mid < 10) id+="0";
				id += mid;
			}else{
				id += "001";
			}
			reportCatalog.setId(id);
			reportCatalogService.save(reportCatalog);
		}
		
		return j;
	}

	/**
	 * 简报目录列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(ReportCatalogEntity reportCatalog, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(reportCatalog.getId())) {
			reportCatalog = reportCatalogService.getEntity(ReportCatalogEntity.class, reportCatalog.getId());
		}
		req.setAttribute("reportCatalogPage", reportCatalog);
		return new ModelAndView("jeecg/business/report/reportCatalog");
	}
}
